home *** CD-ROM | disk | FTP | other *** search
/ Total Network Tools 2002 / NextStepPublishing-TotalNetworkTools2002-Win95.iso / Archive / Misc Servers / Zope.exe / SQLVAR.PY < prev    next >
Encoding:
Python Source  |  2000-09-07  |  8.2 KB  |  215 lines

  1. ##############################################################################
  2. # Zope Public License (ZPL) Version 1.0
  3. # -------------------------------------
  4. # Copyright (c) Digital Creations.  All rights reserved.
  5. # This license has been certified as Open Source(tm).
  6. # Redistribution and use in source and binary forms, with or without
  7. # modification, are permitted provided that the following conditions are
  8. # met:
  9. # 1. Redistributions in source code must retain the above copyright
  10. #    notice, this list of conditions, and the following disclaimer.
  11. # 2. Redistributions in binary form must reproduce the above copyright
  12. #    notice, this list of conditions, and the following disclaimer in
  13. #    the documentation and/or other materials provided with the
  14. #    distribution.
  15. # 3. Digital Creations requests that attribution be given to Zope
  16. #    in any manner possible. Zope includes a "Powered by Zope"
  17. #    button that is installed by default. While it is not a license
  18. #    violation to remove this button, it is requested that the
  19. #    attribution remain. A significant investment has been put
  20. #    into Zope, and this effort will continue if the Zope community
  21. #    continues to grow. This is one way to assure that growth.
  22. # 4. All advertising materials and documentation mentioning
  23. #    features derived from or use of this software must display
  24. #    the following acknowledgement:
  25. #      "This product includes software developed by Digital Creations
  26. #      for use in the Z Object Publishing Environment
  27. #      (http://www.zope.org/)."
  28. #    In the event that the product being advertised includes an
  29. #    intact Zope distribution (with copyright and license included)
  30. #    then this clause is waived.
  31. # 5. Names associated with Zope or Digital Creations must not be used to
  32. #    endorse or promote products derived from this software without
  33. #    prior written permission from Digital Creations.
  34. # 6. Modified redistributions of any form whatsoever must retain
  35. #    the following acknowledgment:
  36. #      "This product includes software developed by Digital Creations
  37. #      for use in the Z Object Publishing Environment
  38. #      (http://www.zope.org/)."
  39. #    Intact (re-)distributions of any official Zope release do not
  40. #    require an external acknowledgement.
  41. # 7. Modifications are encouraged but must be packaged separately as
  42. #    patches to official Zope releases.  Distributions that do not
  43. #    clearly separate the patches from the original work must be clearly
  44. #    labeled as unofficial distributions.  Modifications which do not
  45. #    carry the name Zope may be packaged in any form, as long as they
  46. #    conform to all of the clauses above.
  47. # Disclaimer
  48. #   THIS SOFTWARE IS PROVIDED BY DIGITAL CREATIONS ``AS IS'' AND ANY
  49. #   EXPRESSED OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
  50. #   IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
  51. #   PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL DIGITAL CREATIONS OR ITS
  52. #   CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
  53. #   SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
  54. #   LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF
  55. #   USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
  56. #   ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
  57. #   OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT
  58. #   OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
  59. #   SUCH DAMAGE.
  60. # This software consists of contributions made by Digital Creations and
  61. # many individuals on behalf of Digital Creations.  Specific
  62. # attributions are listed in the accompanying credits file.
  63. ##############################################################################
  64. '''Inserting values with the 'sqlvar' tag
  65.  
  66.     The 'sqlvar' tag is used to type-safely insert values into SQL
  67.     text.  The 'sqlvar' tag is similar to the 'var' tag, except that
  68.     it replaces text formatting parameters with SQL type information.
  69.  
  70.     The sqlvar tag has the following attributes:
  71.  
  72.       name -- The name of the variable to insert. As with other
  73.               DTML tags, the 'name=' prefix may be, and usually is,
  74.               ommitted.
  75.  
  76.       type -- The data type of the value to be inserted.  This
  77.               attribute is required and may be one of 'string',
  78.               'int', 'float', or 'nb'.  The 'nb' data type indicates a
  79.               string that must have a length that is greater than 0.
  80.  
  81.       optional -- A flag indicating that a value is optional.  If a
  82.                   value is optional and is not provided (or is blank
  83.                   when a non-blank value is expected), then the string
  84.                   'null' is inserted.
  85.  
  86.     For example, given the tag::
  87.  
  88.       <dtml-sqlvar x type=nb optional>
  89.  
  90.     if the value of 'x' is::
  91.  
  92.       Let\'s do it
  93.  
  94.     then the text inserted is:
  95.  
  96.       'Let''s do it'
  97.  
  98.     however, if x is ommitted or an empty string, then the value
  99.     inserted is 'null'.
  100. '''
  101. __rcs_id__='$Id: sqlvar.py,v 1.9.38.2 2000/09/07 17:03:13 brian Exp $'
  102.  
  103. ############################################################################
  104. #     Copyright 
  105. #
  106. #       Copyright 1996 Digital Creations, L.C., 910 Princess Anne
  107. #       Street, Suite 300, Fredericksburg, Virginia 22401 U.S.A. All
  108. #       rights reserved.
  109. #
  110. ############################################################################ 
  111. __version__='$Revision: 1.9.38.2 $'[11:-2]
  112.  
  113. from DocumentTemplate.DT_Util import ParseError, parse_params, name_param
  114. from string import find, split, join, atoi, atof
  115. StringType=type('')
  116.  
  117. str=__builtins__['str']
  118.  
  119. class SQLVar: 
  120.     name='sqlvar'
  121.  
  122.     def __init__(self, args):
  123.         args = parse_params(args, name='', expr='', type=None, optional=1)
  124.  
  125.         name,expr=name_param(args,'sqlvar',1)
  126.         if expr is None: expr=name
  127.         else: expr=expr.eval
  128.         self.__name__, self.expr = name, expr
  129.  
  130.         self.args=args
  131.         if not args.has_key('type'):
  132.             raise ParseError, ('the type attribute is required', 'dtvar')
  133.         t=args['type']
  134.         if not valid_type(t):
  135.             raise ParseError, ('invalid type, %s' % t, 'dtvar')
  136.  
  137.     def render(self, md):
  138.         name=self.__name__
  139.         args=self.args
  140.         t=args['type']
  141.         try:
  142.             expr=self.expr
  143.             if type(expr) is type(''): v=md[expr]
  144.             else: v=expr(md)
  145.         except:
  146.             if args.has_key('optional') and args['optional']:
  147.                 return 'null'
  148.             if type(expr) is not type(''):
  149.                 raise
  150.             raise 'Missing Input', 'Missing input variable, <em>%s</em>' % name
  151.  
  152.         if t=='int':
  153.             try:
  154.                 if type(v) is StringType:
  155.                     if v[-1:]=='L':
  156.                         v=v[:-1]
  157.                     atoi(v)
  158.                 else: v=str(int(v))
  159.             except:
  160.                 if not v and args.has_key('optional') and args['optional']:
  161.                     return 'null'
  162.                 raise ValueError, (
  163.                     'Invalid integer value for <em>%s</em>' % name)
  164.         elif t=='float':
  165.             try:
  166.                 if type(v) is StringType:
  167.                     if v[-1:]=='L':
  168.                         v=v[:-1]
  169.                     atof(v)
  170.                 else: v=str(float(v))
  171.             except:
  172.                 if not v and args.has_key('optional') and args['optional']:
  173.                     return 'null'
  174.                 raise ValueError, (
  175.                     'Invalid floating-point value for <em>%s</em>' % name)
  176.         else:
  177.             v=str(v)
  178.             if not v and t=='nb':
  179.                 if args.has_key('optional') and args['optional']:
  180.                     return 'null'
  181.                 else:
  182.                     raise ValueError, (
  183.                         'Invalid empty string value for <em>%s</em>' % name)
  184.             
  185.             v=md.getitem('sql_quote__',0)(v)
  186.             #if find(v,"\'") >= 0: v=join(split(v,"\'"),"''")
  187.             #v="'%s'" % v
  188.  
  189.         return v
  190.  
  191.     __call__=render
  192.  
  193. valid_type={'int':1, 'float':1, 'string':1, 'nb': 1}.has_key
  194.